var baseUrl = "/";
// var baseUrl = "http://localhost:1450/";

$(document).ready(function () {
  $("#dialog").hide();
  initSelect();
  clearSearch();
  initSearchTable();
  //初始化时间选择器
  jeDate("#startTime", {
    skinCell: "jedateblue",
    isinitVal: false,
    initAddVal: [0],
    festival: false,
    format: "YYYY-MM-DD hh:mm:ss",
  });
  jeDate("#endTime", {
    skinCell: "jedateblue",
    isinitVal: false,
    initAddVal: [0],
    festival: false,
    format: "YYYY-MM-DD hh:mm:ss",
  });
});

function initSelect() {
  $.ajax({
    url: baseUrl + "log/getLogTypes",
    contentType: "application/json; charset=utf-8",
    headers: {
      Accept: "application/json",
    },
    success: function (res) {
      for (var i = 0; i < res.length; i++) {
        var type = res[i];
        $("#logType").append(
          "<option value='" + type.value + "'>" + type.name + "</option>"
        );
      }
    },
  });
}

var qyKey = {};

function initSearchTable() {
  getSearchKey();
  console.log("initSearchTable: " + $("#searchBar").outerHeight());
  document.querySelector("table").GM({
    height: "100%",
    ajaxHeaders: {
      "Content-Type": "application/json; charset=utf-8",
      Accept: "application/json",
    },
    gridManagerName: "log_table",
    ajaxData: baseUrl + "log/query",
    ajaxType: "Get",
    query: qyKey, // 传递请求参数
    supportAjaxPage: true,
    sizeData: [10, 20, 30, 50, 100],
    pageSize: 10,
    currentPageKey: "current",
    pageSizeKey: "size",
    useWordBreak: true,
    supportAutoOrder: false,
    columnData: [
      {
        key: "logType",
        width: "90px",
        align: "center",
        text: "分类",
        template: tableItem,
      },
      {
        key: "thread",
        width: "90px",
        align: "center",
        text: "线程",
        template: tableItem,
      },
      {
        key: "className",
        width: "120px",
        align: "center",
        text: "类名",
        template: tableItem,
      },
      {
        key: "method",
        width: "120px",
        align: "center",
        text: "方法",
        template: tableItem,
      },
      {
        key: "time",
        width: "70px",
        align: "center",
        text: "耗时",
        template: tableItem,
      },
      {
        key: "ip",
        width: "100px",
        align: "center",
        text: "客户地址",
        template: tableItem,
      },
      {
        key: "param",
        width: "150px",
        align: "center",
        text: "参数",
        template: tableItem,
      },
      {
        key: "header",
        width: "150px",
        align: "center",
        text: "请求头",
        template: tableItem,
      },
      {
        key: "result",
        text: "结果",
        template: tableItem,
      },
      {
        key: "createTime",
        width: "120px",
        align: "center",
        text: "记录时间",
        template: tableItem,
      },
    ],
  });
}

function tableItem(content) {
  if(content===undefined){
    content = "";
  }
  return `<div class="truncate-text" onclick="showLog(this)">${content}</div>`;
}

function getSearchKey() {
  var method = $("#method").val();
  var ip = $("#ip").val();
  var param = $("#param").val();
  var header = $("#header").val();
  var result = $("#result").val();
  var thread = $("#thread").val();
  var startTime = $("#startTime").val();
  var endTime = $("#endTime").val();
  var domain = $("#domain").val();
  var logType = $("#logType").val();
  var timeAsc = $("input:radio[name='sort']").get(1).checked;

  qyKey = {
    method: method,
    ip: ip,
    param: param,
    header: header,
    result: result,
    logType: logType,
    thread: thread,
    startTime: startTime,
    endTime: endTime,
    domain: domain,
    timeAsc: timeAsc,
  };
  console.log(qyKey);
}

function searchTable() {
  getSearchKey();
  // 更新查询条件
  GM.setQuery("log_table", qyKey);
}

function clearSearch() {
  $("#method").val("");
  $("#ip").val("");
  $("#param").val("");
  $("#header").val("");
  $("#result").val("");
  $("#thread").val("");
  $("#domain").val("0");
  $("#logType").val("");
  $("input:radio[name='sort']").get(0).checked = true;
  //初始化时间
  var now = new Date();
  // 格式化日期时间
  var year = now.getFullYear();
  var month = ("0" + (now.getMonth() + 1)).slice(-2);
  var day = ("0" + now.getDate()).slice(-2);
  var startTime = year + "-" + month + "-" + day + " 00:00:00";
  var endTime = year + "-" + month + "-" + day + " 23:59:59";
  $("#startTime").val(startTime);
  $("#endTime").val(endTime);
}

function showStatus() {
  //获得当前日志收集状态
  showDialog("查询状态中...");
  $.ajax({
    url: baseUrl + "log/getLogSaveStatus",
    contentType: "application/json; charset=utf-8",
    headers: {
      Accept: "application/json",
    },
    success: function (res) {
      $("#dialog_content").html("上次传输时间:"+res.lastTime+
          "<br/>未写入日志数:"+res.queueSize+
          "<br/>队列存满次数:"+res.queueFullCount+
          "<br/>写入错误次数:"+res.insertErrorCount+
          "<br/>写入日志总数:"+res.totalSavedCount+
          "<br/>线程是否关闭:"+res.shuttingDown);
    },
  });
}

function showLog(obj) {
  showDialog(obj.innerHTML);
}

function showDialog(text) {
  $("#dialog").fadeIn();
  $("#dialog_content").html(text);
}

function closeDialog() {
  $("#dialog").fadeOut();
}
